<?php

class ReferenceController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    /*
     * Показываем секцию
     */

    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $page = (int) $this->getRequest()->getParam('page', 1);
        $tid = (int) $this->getRequest()->getParam('tag', 0);
        $model = new ReferencesModel();
        $sref = $model->getSectionRef($sid);
        $rid = $sref->rid;

        if ($rid > 0) {
            $forms = new FormsModel();
            $ref = $model->getRefs($rid);
            $this->view->ref = $ref;
            $this->view->fields = $forms->getFields($ref['form']);
            $id = (int) $this->getRequest()->getParam($ref['param'], 0);
            if ($tid > 0) {
                $tagsModel = new TagsModel();
                $tag = $tagsModel->getTagById($tid);
                if ($tag)
                    $this->view->tag = $tag;
            }
            if ($id > 0) {
                if ($ref['connection']) {
                    $this->view->records = $model->getPaginatorRows($ref, $this->view->fields, null, $sid, $id, $tid);
                } else {
                    $this->view->records = $model->getPaginatorRows($ref, $this->view->fields, null, null, $id, $tid);
                }
                foreach ($this->view->records as $r) {
                    $this->view->record = $r;
                    $breadcrumbs = Zend_Registry::set('breadcrumbs', array(array('name' => $r->$ref['breadcrumb'], 'url' => PAGE_URL . '/' . $ref['param'] . '/' . $id . '/page/' . $page)));
                    break;
                }
            } else {
                if ($ref['connection']) {
                    $this->view->records = $model->getPaginatorRows($ref, $this->view->fields, $page, $sid, null, $tid);
                } else {
                    $this->view->records = $model->getPaginatorRows($ref, $this->view->fields, $page, null, null, $tid);
                }
            }
        } else {
            $this->_helper->viewRenderer->setNoRender();
        }
    }

    /*
     * Редактируем секцию
     */

    public function modifyAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $action = $this->getRequest()->getParam('a');
        if ($action == 'record') {
            $this->_forward('record');
            return true;
        } elseif ($action == 'remove') {
            $this->_forward('remove');
            return true;
        }
        $model = new ReferencesModel();
        $sref = $model->getSectionRef($sid);

        if ($sref->rid > 0) {
            $formsModel = new FormsModel();
            $ref = $model->getRefs($sref->rid);
            $this->view->ref = $ref;
            $this->view->fields = $formsModel->getFields($ref['form']);
            if ($ref['connection']) {
                $this->view->records = $model->getRecords($ref, $this->view->fields, $sid);
            } else {
                $this->view->records = $model->getRecords($ref, $this->view->fields);
            }
        }

        $forms = new ReferencesForm();
        $form = $forms->pick($sid);
        $this->view->form = $form;

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            $data = array('rid' => (int) $formData['rid']);
            $model->updateSectionRef($data, $sid);
            $this->_redirect('/pages/modify/id/' . $sid);
        } else {
            $form->populate((array) $sref);
        }
    }

    public function recordAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $rid = (int) $this->getRequest()->getParam('rid', 0);
        $id = (int) $this->getRequest()->getParam('recid', 0);
        $t = Zend_Registry::get('Zend_Locale');
        $model = new ReferencesModel();
        $ref = $model->getRefs($rid);
        $forms = new FormsForm();
        $this->view->ref = $ref;

        if ($id > 0) {
            $record_data = $model->getRecord($ref, $id);
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('Edit record') => ADMIN_URL . '/pages/modify/a/record/rid/' . $rid . '/id/' . $sid . '/recid/' . $id);
            $this->view->record = $record_data;
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('New record') => ADMIN_URL . '/pages/modify/a/record/rid/' . $rid . '/id/' . $sid . '/recid/0');
        }

        $form = $forms->getForm($ref['form'], ADMIN_URL . '/pages/modify/a/record/rid/' . $rid . '/id/' . $sid . '/recid/' . $id);
        $this->view->form = $form;

        $formsmodel = new FormsModel();
        $fields = $formsmodel->getFields($ref['form']);

        if ($this->_request->isPost()) {
            $Data = $this->_request->getPost();
            if ($form->isValid($Data)) {
                unset($Data['submit']);
                foreach ($fields as $key => $value) {

                    if ($value->type == 'date')
                        $Data[$value->field] = date('Y-m-d', strtotime($Data[$value->field]));

                    if ($value->type == 'image') {
                        $imagesModel = new ImagesModel();
                        $imagesModel->updateImage($Data[$value->field], $value->image_width, $value->image_height, $value->preview, $value->preview_width, $value->preview_height);
                    }

                    if ($value->type == 'tags') {
                        $tagsModel = new TagsModel();
                        $tagsModel->updateTags($Data[$value->field], $value->identifier, $id);
                        $Data[$value->field] = '';
                    }
                }
                if ($ref['connection'])
                    $Data['sid'] = $sid;
                $id = $model->updateRecord($ref['table'], $Data, $id);
                $this->_redirect('/pages/modify/id/' . $sid);
            } else {
                $form->populate($Data);
            }
        } else {
            if ($id > 0) {
                foreach ($fields as $key => $value) {
                    if ($value->type == 'tags') {
                        $tagsModel = new TagsModel();
                        $record_data[$value->field] = $tagsModel->getObjectTags($value->identifier, $id, true);
                    }
                }
                $form->populate($record_data);
            }
        }
    }

    /*
     * delete record
     */

    public function removeAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $rid = (int) $this->getRequest()->getParam('rid', 0);
        $id = (int) $this->getRequest()->getParam('recid', 0);
        $model = new ReferencesModel();
        $ref = $model->getRefs($rid);
        $model->deleteRecord($ref['table'], $id);
        $this->_redirect('/pages/modify/id/' . $sid);
    }

    /*
     * Добавляем секцию
     */

    public function addAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pages_model = new PageModel();
        $section = $pages_model->getSection($sid);
        $pid = $section->pid;
        $model = new ReferencesModel();
        $model->updateSectionRef(array('sid' => $sid));
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
